//
//  DetailReplyTableViewCell.m
//  Comma
//
//  Created by 张诗雨 on 15/1/22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailReplyTableViewCell.h"
#import "GlobalMacro.h"
#import "FaceModel.h"
#import "SETextView.h"
#import "EditImage.h"
#import "GlobalMethod.h"
@implementation DetailReplyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 设置选中效果
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        self.infoDic = [NSMutableDictionary dictionary];
        self.replyButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.replyButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
        [self.replyButton setFrame:CGRectMake(24, 0, 0, 0)];
        [self.replyButton addTarget:self action:@selector(replyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.replyButton];
        
        self.reply = [[UILabel alloc] init];
        self.reply.font = [UIFont systemFontOfSize:14.0f];
        [self.reply setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]];
            [self.reply setTextColor:TEXTERGRAY];
        [self.reply setText:@"回复"];
        [self.contentView addSubview:self.reply];
        
        self.hostButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.hostButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.hostButton addTarget:self action:@selector(hostButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.hostButton];
        
        self.replyLabel = [[SETextView alloc] initWithFrame:CGRectMake(24, 6, 0, 0)];
        self.replyLabel.font = [UIFont systemFontOfSize:14.0f];
        [self.replyLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0f]];
        [self.replyLabel setTextColor:TEXTERGRAY];
//        [self.replyLabel setNumberOfLines:0];
        [self.contentView addSubview:self.replyLabel];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.replyButton sizeToFit];
    [self.reply setFrame:CGRectMake(self.replyButton.frame.size.width + 27, 6, 0, 0)];
    [self.reply sizeToFit];
    [self.hostButton setFrame:CGRectMake(self.reply.frame.origin.x + self.reply.frame.size.width + 3, 0, 0, 0)];
    [self.hostButton sizeToFit];
    NSString *text = [self.infoDic objectForKey:@"content"];
//    NSArray * array = [text componentsSeparatedByString:@"<img src=\"http://isu.so"];
//    NSMutableArray * resultArr = [NSMutableArray array];
//    for (NSString * element in array) {
//        if ([element hasPrefix:@"/upload/emotion/motion"]) {
//            NSArray * arr = [element componentsSeparatedByString:@"\" alt=\"cool\" width=\"32px\" height=\"32px\"/>"];
//            NSString * new = [arr firstObject];
//            [resultArr insertObject:[FaceModel getImageByString:new] atIndex:0];
//        }
//        else
//        {
//            NSString *string = [GlobalMethod replaceCharacterWithString:element];
//            NSAttributedString * att = [[NSAttributedString alloc] initWithString:string];
//            [resultArr insertObject:att atIndex:0];
//        }
//    }
    NSArray * resultArr = [FaceModel getImageByString:text];
    NSLog(@"%@", resultArr);
    NSInteger count = [resultArr count];
    for (int i = count - 1; i >= 0; i --) {
        id element = [resultArr objectAtIndex:i];
        if ([element isKindOfClass:[NSAttributedString class]]) {
            [self.replyLabel insertAttributedText:element];
        }
        else if ([element isKindOfClass:[UIImage class]]) {
            EditImage * image = [[EditImage alloc] initWithData:UIImageJPEGRepresentation(element, 1.0)];
            image.name = @"123";
            [self.replyLabel insertObject:image size:CGSizeMake(35, 35)];
        }
    }
    [self.timeLabel sizeToFit];
    CGRect rect = _timeLabel.frame;
    [self.timeLabel setFrame:CGRectMake(SCREEN_WIDTH - rect.size.width - 8, _replyLabel.frame.origin.y, rect.size.width, _replyLabel.frame.size.height)];
    CGSize containerSize = CGSizeMake(SCREEN_WIDTH, 10000);
    CGSize contentSize = [self.replyLabel sizeThatFits:containerSize];
    self.replyLabel.frame = CGRectMake(self.replyButton.frame.origin.x, self.replyButton.frame.origin.y + self.replyButton.frame.size.height, SCREEN_WIDTH - _timeLabel.frame.size.width, contentSize.height);

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)replyButtonAction:(id)sender
{
    [self.delegate detailReplyAction:[self.infoDic objectForKey:@"puberId"]];
}

- (void)hostButtonAction:(id)sender
{
    [self.delegate detailReplyAction:[self.infoDic objectForKey:@"replytoId"]];
}

@end
